package com.zj.jdbc2ThreadLocal.tools;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class ServiceInterceptor implements MethodInterceptor {
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        //o表示增强的对象，即实现这个接口类的一个对象
        System.out.println(method);
        System.out.println(methodProxy);
        Object o1=null;
        try{
            JdbcThreadLocal.startTransaction();
            o1 = methodProxy.invokeSuper(o, objects);
            JdbcThreadLocal.commit();
        }catch (Exception e){
            e.printStackTrace();
            JdbcThreadLocal.rollback();
        }finally {
            JdbcThreadLocal.close();
        }
        return o1;
    }
}
